package com.example.demo.service;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.mapper.PsPointMapper;
import com.example.demo.model.PsPointN;
import com.example.demo.repository.PointRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PointServiceImpl implements PointRepository {

    @Autowired
    private PsPointMapper psPointMapper;

    @Qualifier("psPointMapper")
    @Autowired
    private BaseMapper baseMapper;


    public List<PsPointN> getPoint(Double coorx, Double coory, Integer distance) {
        List<PsPointN> pointList = psPointMapper.selectByPoint(coorx, coory, distance);
        return pointList;
    }

    public List<PsPointN> getPointByExtent(Double minx, Double miny, Double maxx, Double maxy) {
        System.out.println(System.currentTimeMillis());
        List<PsPointN> pointList = psPointMapper.selectByExtend(minx, miny, maxx, maxy);
        System.out.println(System.currentTimeMillis());

        return pointList;
    }

    public List<PsPointN> getPointByWkt(String wkt, Integer page) {
        List<PsPointN> pointList = psPointMapper.selectByPolygon(wkt, page);

        return pointList;
    }

    public Integer getCountByWkt(String wkt) {
//        System.out.println(System.currentTimeMillis());
        Integer count = psPointMapper.getCountByPolygon(wkt);
//        System.out.println(System.currentTimeMillis());

        return count;
    }


    public Boolean updateGeom(String survey_no, String geom) {
        try {
            PsPointN entity = new PsPointN();
            entity.setGeom(geom);
            UpdateWrapper<PsPointN> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("survey_no", survey_no);
            int rows = baseMapper.update(entity, updateWrapper);
            return rows > 0;
        } catch (Exception exception) {
            return false;
        }

    }
}
